Ext.define('CS.tool.PagingToolbar', {
	extend: 'Ext.PagingToolbar',
	requires: ['CS.data.CustomStore'],

	prependButtons: true,

	displayInfo: true,

	displayMsg: '{0} - {1} 共 {2}',

	pageSizeData: undefined,

	defaultPageSizeData: [[10, 10], [20, 20], [50, 50], [100, 100]],

	store: undefined,

	pageSize: undefined,

	constructor: function(config) {
		var me = this;

		config = CS.apply({}, config);

		config.pageSizeData = [];
		CS.apply(config.pageSizeData, me.defaultPageSizeData);

		var tempNum = 0;
		for (; tempNum < config.pageSizeData.length; tempNum++) {
			var arr = config.pageSizeData[tempNum];

			if (arr[0] == config.pageSize) {
				tempNum = -1;
				break;
			} else if (arr[0] > config.pageSize) {
				break;
			}
		}
		if (tempNum > -1) {
			config.pageSizeData.splice(tempNum, 0, [config.pageSize, config.pageSize]);
		}

		config.items = [{
			xtype: 'field.combo',
			width: 80,
			fields: 'code,description',
			localData: config.pageSizeData,
			value: config.pageSize,
			needTextTpl: false,
			listeners: {
				change: function(conbo, value) {
					me.reload(value);
				}
			}
		}];

		me.callParent([config]);
	},

	reload: function(pageSize) {
		this.store.pageSize = pageSize;
		this.store.loadPage(1, this.store.lastParams);
	},

	moveFirst: function() {
		if (this.fireEvent('beforechange', this, 1) !== false) {
			this.store.loadPage(1, this.store.lastParams);
		}
	},

	movePrevious: function() {
		var prev = this.store.currentPage - 1;

		if (prev > 0) {
			if (this.fireEvent('beforechange', this, prev) !== false) {
				this.store.previousPage(this.store.lastParams);
			}
		}
	},

	moveNext: function() {
		var next = this.store.currentPage + 1;

		if (next <= this.getPageData().pageCount) {
			if (this.fireEvent('beforechange', this, next) !== false) {
				this.store.nextPage(this.store.lastParams);
			}
		}
	},

	moveLast: function() {
		var last = this.getPageData().pageCount;

		if (this.fireEvent('beforechange', this, last) !== false) {
			this.store.loadPage(last, this.store.lastParams);
		}
	},

	doRefresh: function() {
		var current = this.store.currentPage;

		if (this.fireEvent('beforechange', this, current) !== false) {
			this.store.loadPage(current, this.store.lastParams);
		}
	}

});
